package com.example.mycityapp.util;

import java.io.InputStream;
import java.lang.ref.WeakReference;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import com.example.mycityapp.MainActivity;
import com.example.mycityapp.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class ImageDownloader {

	public static void download(String url, ImageView imageView, boolean fixSize) {
		if (MainActivity.dontLoadImages)
		{
			imageView.setImageResource(R.drawable.ic_launcher);
		}
		else
		{
			BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, fixSize);
			task.execute(url);
		}
	}
}

class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
	private String url;
	boolean fixSize=false;
	private final WeakReference<ImageView> imageViewReference;

	public BitmapDownloaderTask(ImageView imageView, boolean fixSize) {
		imageViewReference = new WeakReference<ImageView>(imageView);
		this.fixSize=fixSize;
	}

	@Override
	// Actual download method, run in the task thread
	protected Bitmap doInBackground(String... params) {
		// params comes from the execute() call: params[0] is the url.
		return downloadBitmap(params[0], fixSize);
	}

	@Override
	// Once the image is downloaded, associates it to the imageView
	protected void onPostExecute(Bitmap bitmap) {
		if (isCancelled()) {
			bitmap = null;
		}

		if (imageViewReference != null) {
			ImageView imageView = imageViewReference.get();
			if (imageView != null) {
				imageView.setImageBitmap(bitmap);
			}
		}
	}

	static Bitmap downloadBitmap(String url, boolean fixSize) {
		final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
		final HttpGet getRequest = new HttpGet(url);

		try {
			HttpResponse response = client.execute(getRequest);
			final int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != HttpStatus.SC_OK) { 
				Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
				return null;
			}

			final HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream inputStream = null;
				try {
					inputStream = entity.getContent(); 
					final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
					final Bitmap resized = Bitmap.createScaledBitmap(bitmap, 620, 620, true);
					if(fixSize)
						return resized;
					else
						return bitmap;
				} finally {
					if (inputStream != null) {
						inputStream.close();  
					}
					entity.consumeContent();
				}
			}
		} catch (Exception e) {
			// Could provide a more explicit error message for IOException or IllegalStateException
			getRequest.abort();
			Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
		} finally {
			if (client != null) {
				client.close();
			}
		}
		return null;
	}
}
